package com.example.mall.web.controller;

import com.example.mall.domain.UserCoupon;
import com.example.mall.dto.UserCouponDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserCouponQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IUserCouponService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userCoupon")
public class UserCouponController {

    @Autowired
    public IUserCouponService userCouponService;

    /**
     * 分页查询用户优惠券表信息
     * @param userCouponQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询用户优惠券表信息",notes = "分页查询用户优惠券表信息",httpMethod = "GET")
    public JSONResult queryPage(UserCouponQuery userCouponQuery){
        PageDTO<UserCouponDto> userCouponDtoPageDTO = userCouponService.queryPage(userCouponQuery);
        return JSONResult.success("查询成功",userCouponDtoPageDTO);
    }

    /**
     * 根据id查询用户优惠券表信息
     * @param userCouponId
     * @return
     */
    @GetMapping("query/{userCouponId}")
    @ApiOperation(value = "根据id查询用户优惠券表信息",notes = "根据id查询用户优惠券表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("userCouponId")Long userCouponId){
        //1.根据id查询用户信息
        if (userCouponId == null){
            throw new GlobalCustomerException(ErrorEnumCode.USERCOUPONID_NOT_ID);
        }
        UserCoupon userCoupon = userCouponService.getById(userCouponId);
        return JSONResult.success("查询成功",userCoupon);
    }

    /**
     * 根据id查询用户优惠券表信息
     * @param userCouponId
     * @return
     */
    @GetMapping("queryUserCouponById/{userCouponId}")
    @ApiOperation(value = "根据id查询用户优惠券表信息",notes = "根据id查询用户优惠券表信息",httpMethod = "GET")
    public JSONResult queryLoginById(@PathVariable("userCouponId")Long userCouponId){
        //1.根据id查询用户信息
        if (userCouponId == null){
            throw new GlobalCustomerException(ErrorEnumCode.USERCOUPONID_NOT_ID);
        }
        UserCoupon userCoupon = userCouponService.queryLoginById(userCouponId);
        return JSONResult.success("查询成功",userCoupon);
    }

    /**
     * 查询全部用户优惠券表信息
     * @param
     * @return
     */
    @GetMapping("queryUserCouponList")
    @ApiOperation(value = "查询全部用户优惠券表信息",notes = "查询全部用户优惠券表信息",httpMethod = "GET")
    public JSONResult queryLoginList(){
        List<UserCoupon> logins = userCouponService.queryLoginList();
        return JSONResult.success("查询成功",logins);
    }

    /**
     * 新增用户优惠券表
     * @param userCouponDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增用户优惠券表",notes = "新增用户优惠券表",httpMethod = "POST")
    public JSONResult insert(@RequestBody UserCouponDto userCouponDto){
        userCouponService.insert(userCouponDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改用户优惠券表信息
     * @param userCouponDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改用户优惠券表信息",notes = "修改用户优惠券表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody UserCouponDto userCouponDto){
        userCouponService.update(userCouponDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除用户优惠券表信息
     * @param userCouponId
     * @return
     */
    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除用户优惠券表信息",notes = "删除用户优惠券表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("userCouponId") Long userCouponId) {
        userCouponService.delete(userCouponId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除用户优惠券表信息
     * @param userCouponIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除用户优惠券表信息",notes = "批量删除用户优惠券表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> userCouponIds) {
        userCouponService.deleteBatch(userCouponIds);
        return JSONResult.success("批量删除成功");
    }
}
